package utils

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"strings"
)

func Y易源码替换APi接口并修复(易源码 []byte, APi接口列表 []string, 加密盐 string) []byte {
	if len(加密盐) == 0 {
		return 易源码
	}

	局_易源码字节集 := 易源码
	for _, api := range APi接口列表 {
		hash := md5.Sum([]byte(api + 加密盐))
		局_ApiMd5 := []byte(hex.EncodeToString(hash[:]))
		局_字节集1 := 转换字节集并补齐32位(api)
		局_易源码字节集 = bytes.Replace(局_易源码字节集, 局_字节集1, 局_ApiMd5, 1)
	}
	//E.E写到文件("E:\\yun\\xuhaonan\\project\\TY通用后台管理系统\\对接HttpApi\\飞鸟快验对接源码APi加密盐"+加密盐+"易修复.e", 局_易源码字节集)

	/*	局_字节集头, _ := hex.DecodeString("71000000511b")
		//局_开始 := bytes.Index(局_易源码字节集, 局_字节集头) - 4         //{0,0,0,0}   bytes.Index 有坑不能用,会寻找不到
		局_开始 := 寻找字节集(局_易源码字节集, 局_字节集头) - 4               //{0,0,0,0}
		局_结束 := 寻找字节集(局_易源码字节集, 局_字节集尾) + len(局_字节集尾) + 5 //{0,0,0,0,0} 正常7009 长度  Info.DataLength 值
		if 局_开始 < 0 {
			局_开始 = 511
		}*/
	局_开始 := 511
	局_结束 := 7009
	if len(局_易源码字节集) < 局_开始+局_结束 {
		return []byte{}
	}

	局_待校验字节集 := 局_易源码字节集[局_开始 : 局_开始+局_结束]
	//0000000071000000511B0000C6310118043F0118033F0118B3380118B4380118B6380118B7380118B8380118B9380118BA380118BB380118BC380118093001180A3001180B3001180C3001180D3001180E3001180F300118103001181130011812300118133001181430011815300118163001181730011818300118193001181A300118BE400118BF400118FF4001180041011801410118024101185741011858410118594101185A4101185B410118C2410118C3410118CA410118CB410118CC410118CD410118D6410118E9410118F0410118FB410118FC410118FD410118104201181242011853420118544201185542011899430118DF430118A4430118A5430118A6430118A7430118A8430118A9430118AA430118AB430118AC430118AD430118AE430118AF430118B0430118B1430118B2430118B3430118B4430118B5430118B6430118B7430118B8430118B9430118BA430118BB430118BC430118BD430118BE430118BF430118C0430118C1430118C2430118C3430118C4430118C5430118C6430118C7430118C8430118C9430118CA430118CB430118CC430118CD430118CE430118CF430118D0430118D1430118D2430118D3430118D4430118D5430118D6430118D7430118D843011800000000870100002003000029030000320300003B03000095030000CB0300003E04000078040000AC040000E5040000EE0400001C050000410500006E05000099050000C2050000ED050000150600003E0600006406000088060000A4060000C7060000EF06000013070000400700006A07000094070000C1070000ED07000014080000310800004E0800006B08000084080000A3080000C2080000E1080000000900001F0900003C0900005A0900007E09000093090000B4090000D5090000F60900000E0A00002A0A0000400A0000580A0000730A00009A0A0000B80A00001B0B0000400B0000770B0000800B0000890B0000E00B0000170C00004E0C00008B0C0000CC0C00000B0D0000450D00007A0D0000B20D0000ED0D00002B0E0000690E0000A40E0000DF0E00001A0F0000550F0000900F0000C80F0000031000003E10000075100000B1100000EA1000002B110000641100009F110000DE1100001A1200004E12000088120000BF120000F81200003013000071130000B0130000E3130000171400004B1400008A140000C3140000FB140000351500006F150000AF150000F01500002A16000068160000A2160000D5160000121700004F1700008C170000830100001000B2E2CAD4C5E4D6C3D5CBBAC500001A6E0100007B22417070576562223A22687474703A2F2F3132372E302E302E313A31383838382F4170693F41707049643D3130303031222C2243727970746F4B65795075626C6963223A222D2D2D2D2D424547494E205055424C4943204B45592D2D2D2D2D5C6E4D4947664D413047435371475349623344514542415155414134474E4144434269514B42675144644632414C30504F4471506F564353704530434765713336555C6E6F502F4D79384448545061675567302F6B35595A4E56516D6E685262506D626A444E5457534F2B6E716533536E5A5972776369335335516F382B56634F6C35705C6E33633559316E6D504C324A6151794173684A6F43577657334C565A645264594D4B7757485A3664705133634E6E474C635557504D5A2F625A74456C39667153715C6E4E355A63316A79746F2B356832354E6630774944415141425C6E2D2D2D2D2D454E44205055424C4943204B45592D2D2D2D2D5C6E222C2243727970746F54797065223A337D00950100001000B2E2CAD4C5E4D6C3BFA8BAC500001A800100007B22417070576562223A22687474703A2F2F616E79756579696E6C756F2E65332E6C75796F757869612E6E65743A31333335382F4170693F41707049643D3130303039222C2243727970746F4B65795075626C6963223A222D2D2D2D2D424547494E205055424C4943204B45592D2D2D2D2D5C6E4D4947664D413047435371475349623344514542415155414134474E4144434269514B4267514335644D766C56727A356646345338384635656E757442434A315C6E2B3067625346497A776E646264544352536C706D54773230754631776A7350616B664843707271343643506646337370745551684B524F69636D2F2F472F642B5C6E633331764E543847486A466B714546435177436D4D61415A495170632F44324A6D534B48447743766369473578384A346E367572387942343846364E42432B6B5C6E7368766658444159436842446138702B48774944415141425C6E2D2D2D2D2D454E44205055424C4943204B45592D2D2D2D2D5C6E222C2243727970746F54797065223A337D00050000000000000016050000000000000016050000000000000016560000000000525341CCEEB3E45F5253415F504B4353315F50414444494E4700525341D7EEB3A3D3C3B5C4CCEEB3E4B7BDCABDA3ACBCD3C3DCC4DAC8DDB3A4B6C8B1D8D0EBD0A1D3DAC4A3B3A4BCF5330017000000000000F03F320000000000525341CCEEB3E45F5253415F53534C5632335F50414444494E4700525341CCEEB3E4B7BDCABD001700000000000000406F0000000000525341CCEEB3E45F5253415F4E4F5F50414444494E4700525341CCEEB3E4B7BDCABDA3ACB2BBD3C3CCEEB3E4A3ACC8E7B9FBCAB9D3C3B4CBCFEEA3ACC3F7CEC4BACDC3DCCEC4B3A4B6C8B6BCD2AAB5C8D3DA5253415F73697A6528727361D6B8D5EB2900170000000000000840360000000000525341CCEEB3E45F5253415F504B4353315F4F4145505F50414444494E4700525341CCEEB3E4B7BDCABD00170000000000001040300000000000525341CCEEB3E45F5253415F583933315F50414444494E4700525341CCEEB3E4B7BDCABD00170000000000001440350000000000525341CCEEB3E45F5253415F504B4353315F5053535F50414444494E4700525341CCEEB3E4B7BDCABD001700000000000018400500000000000000162A00000004004A56C0E0D0CD5FCEB4B6A8D2E5004A56545950455F554E444546494E45440017000000000000F0BF2100000004004A56C0E0D0CD5FBFD5004A56545950455F4E554C4C001700000000000000002900000004004A56C0E0D0CD5FCBABBEABB6C8D0CD004A56545950455F444F55424C450017000000000000F03F2700000004004A56C0E0D0CD5FB3A4D5FBCAFDD0CD004A56545950455F4C4F4E47001700000000000000402500000004004A56C0E0D0CD5FC2DFBCADD0CD004A56545950455F424F4F4C001700000000000008402700000004004A56C0E0D0CD5FCEC4B1BED0CD004A56545950455F535452494E47001700000000000010402400000004004A56C0E0D0CD5FCAFDD7E9004A56545950455F4152524159001700000000000014402500000004004A56C0E0D0CD5FB6D4CFF3004A56545950455F4F424A454354001700000000000018402200000004004A56BDE2CEF65F55534332BDE2C2EB005C75787878780017000000000000F03F2000000004004A56BDE2CEF65FBAF6C2D4C7B0BAF3CEC4B1BE00001700000000000000401800000004004A534F4E5F4F4B00B3C9B9A6001700000000000000001F00000004004A534F4E5F4552524F5200CEB4D6AAB4EDCEF30017000000000000F0BF2400000004004A534F4E5F494E56414C494441524700B2CECAFDB4EDCEF3001700000000000000C02000000004004A534F4E5F4D454D4F525900C4DAB4E6B4EDCEF3001700000000000008C02900000004004A534F4E5F494E56414C49444E414D4500CEDED0A7B5C4CAF4D0D4C3FB001700000000000010C02600000004004A534F4E5F4F424A454354504152454E5400B4EDCEF3B5C4B8B8001700000000000014C02600000004004A534F4E5F494E56414C494456414C554500B4EDCEF3B5C4D6B5001700000000000018C02900000004004A534F4E5F4E4F4E45585451554F544500CEDECFC2D2BBB8F6D2FDBAC500170000000000001CC0280000000200B6D4B3C6CBE3B7A85F4145535F3139325F43424300001A0B0000004145533139322D43424300230000000200CAFDBEDDCCEEB3E45F504B4353375F50414444494E470000170000000000001C4019000000020043414C475F4145535F313238000017000000008083D94019000000020043414C475F4145535F31393200001700000000C083D94019000000020043414C475F4145535F323536000017000000000084D94015000000020043414C475F414553000017000000004084D9401B000000020043525950545F4D4F44455F434243000017000000000000F03F1B000000020043525950545F4D4F44455F45434200001700000000000000401B000000020043525950545F4D4F44455F4F464200001700000000000008401B000000020043525950545F4D4F44455F43464200001700000000000010401B000000020043525950545F4D4F44455F435453000017000000000000144019000000020050524F565F5253415F41455300001700000000000038401A000000020050524F565F5253415F46554C4C000017000000000000F03F20000000020043525950545F564552494659434F4E54455854000017000000000000B0C11100000002004E554C4C00001700000000000000001D0000000200504C41494E544558544B4559424C4F4200001700000000000020401D00000002004355525F424C4F425F56455253494F4E00001700000000000000401D000000020043525950545F4558504F525441424C45000017000000000000F03F1400000002004B505F4D4F444500001700000000000010401800000002004B505F424C4F434B4C454E00001700000000000020401200000002004B505F4956000017000000000000F03F1400000002004B505F53414C5400001700000000000000401700000002004B505F50414444494E470000170000000000000840230000000200CAFDBEDDCCEEB3E45F504B4353355F50414444494E4700001700000000000014401A0000000200504B4353355F50414444494E47000017000000000000F03F5F000000020043505F67623233313200414E53492F4F454D2053696D706C6966696564204368696E65736520285052432C2053696E6761706F7265293B204368696E6573652053696D706C696669656420284742323331322900170000000000408D40210000000200494E56414C49445F48414E444C455F56414C5545000017000000000000F0BF33000000020046494C455F4154545249425554455F4449524543544F525900B1EACAB6C4BFC2BCB5C4BEE4B1FA00170000000000003040050000000000000016050000000000000016530000000000476574546F6B656E00CAD7CEB2D3D0BFD5B0D7C3BBB9D8CFB52CB7FECEF1C6F7BBE1C9BECAD7CEB2BFD5001A2100000065643833343634353662356630363931313563316239346361373138633265360033000000000047657455736572495000001A21000000326232336238313636616362633132313136663630613838626335333132663000330000000000557365724C6F67696E00001A21000000633765363462333166366261363031316463383630396436396530623636343500390000000000557365725265647563654D6F6E657900001A210000006461643737333462396561613864633064323937623039393761656262326539003D0000000000557365725265647563655669704E756D62657200001A210000003961663239376230373538616664646264633638626139336530616339353133003B00000000005573657252656475636556697054696D6500001A2100000035616437346663613533623331316461643934666634666132383134636234310036000000000049735365727665724C696E6B00001A2100000061363936316337376466646237303062663862653939393836303638313032610031000000000049734C6F67696E00001A210000003430326130616336393636613734373236376538393339646330646332383837003400000000004765745669704461746100001A21000000663862663534356339383862383961636562343335303861396438333337306500370000000000476574417070476F6E6747616F00001A210000003361343436333266653931303964353731633639656336646637613438373965003A00000000004765744170705570446174614A736F6E00001A210000006439303339323266323038323439353236326130623465336132613434313265003A00000000004765744170705075626C69634461746100001A210000006464333732613162666437616461356465346561636466356134643634643036003700000000004765745075626C69634461746100001A2100000031643734343734336665633639323262643935656333333066323438313034320037000000000047657441707056657273696F6E00001A21000000383837376138353930653330356230363465376534616532306438393135316100370000000000476574417070486F6D6555726C00001A21000000656237643833666331386136383830336236303261366131616230326534346400370000000000536574417070557365724B657900001A210000003237356135383732363635393162616133613065663932386237653563613239003700000000005365744E6577557365724D736700001A210000003366366435393862316230393535663636343361303064326334343131653665003400000000004765744361707463686100001A21000000356630376133366235396534303534396566383164303765333563376661326200370000000000476574534D534361707463686100001A21000000313765326662346263393466616265623464363233393130376535313830633100370000000000476574417070557365724B657900001A2100000063613766663765373866386133336631363461353839383433643536346430640033000000000047657449735573657200001A2100000061653332373836346336666631323635373363333336343632363431393436660038000000000047657441707055736572496E666F00001A2100000039303438393232313762323161633431383632353733376239616437633733310035000000000047657455736572496E666F00001A210000003064373236633162306530396533333337626463323762373731393065636566003D0000000000536574557365725171456D61696C50686F6E6500001A210000006335333935616531373063373637336662306330343336396231356138333430003500000000004E657755736572496E666F00001A2100000065623132613265386230643966313338376161643037663439323034313033350037000000000047657453797374656D54696D6500001A210000003165646238376362383436353338613063306137613238393735343764363939003B00000000004765744170705573657256697054696D6500001A21000000393839666561336538623961336566306536656362383937306166333566666400380000000000476574417070557365724E6F746500001A210000006233643738303863643763313061646166343436646662386264373039346334003000000000004C6F674F757400001A2100000064316537386437396634613763353736303435636335643764386238326535650036000000000052656D6F74654C6F674F757400001A2100000061356535626562663266363665316439643662336433613835636263643662370033000000000048656172744265617400001A2100000065396633363765336662613462643630316461393862376236373166396562340035000000000053657450617373576F726400001A2100000066636132616136336264666461653863323064356134366162336132663239390034000000000047657455736572526D6200001A210000003038336537396336306164396332646133623837666462353965366332373663003D0000000000476574417070557365725669704E756D62657200001A210000006630386631373033363439376339316234653936376439666663636235643862003B0000000000476574436170746368614170694C69737400001A210000003636386431376635653861663936646164656234643238373661386463336237002F00000000005573654B6100001A2100000036653238373035336235666434653632633338373337643933363635643939300030000000000047657454616200001A2100000033303565373639393631303264323635623161303732613764363762396238380030000000000053657454616200001A210000006263363932383564643132366633343138633032313132303838386532393239003B00000000004765745061794F7264657253746174757300001A21000000663033363261383334653062376164623866356133633661393336633638336200350000000000476574416C69506179504300001A210000006231626161613330313136613066316263356233383261323734643039636634003400000000004765745758506179504300001A2100000064663735643935366163643233663933316535306664613831306632306165360036000000000047657450617953746174757300001A210000003261326130303664653865353237656264656236393631626437636539636466003600000000004765745061794B614C69737400001A210000003165383931613430626662396132643233333162343266386235386138356332003C00000000004765745075726368617365644B614C69737400001A210000006532663639653465313935333639303734336536303962663565356532666330003D00000000005061794D6F6E6579546F5669704E756D62657200001A210000003538373630326665623933353161343661393838363663663330666364313965003600000000005061794D6F6E6579546F4B6100001A210000003636343934663733633466346635343732393265383339386466623332386364003A000000000047657455736572436C6173734C69737400001A2100000061653233663435323333653366656234306261666461326664633938383636350036000000000053657455736572436C61737300001A210000003366653737386231313339393236663431636539313563343930303463666532002F000000000052756E4A5300001A210000006638356666353531643739383037343139343564356533386335343238343664003900000000005461736B506F6F6C4E65774461746100001A210000003135666264383832633364633238373831366535616639653233326664366432003900000000005461736B506F6F6C4765744461746100001A210000003637613234646330616533336462616463326638643266616530613432626634003900000000005461736B506F6F6C4765745461736B00001A210000006533363466626631303133393030366565366361633231333737646561666637003900000000005461736B506F6F6C5365745461736B00001A2100000035303638653163393032363665656634353133363266306161363237313139350000000000
	fmt.Printf("1校验data结果:" + strings.ToUpper(hex.EncodeToString(局_待校验字节集)) + "\n")
	局_第一个程序资源段校验 := GetCheckValue(局_待校验字节集)
	fmt.Printf("1校验值结果:" + strings.ToUpper(hex.EncodeToString(局_第一个程序资源段校验)) + "\n")
	局_易源码字节集[459] = 局_第一个程序资源段校验[0]
	局_易源码字节集[460] = 局_第一个程序资源段校验[1]
	局_易源码字节集[461] = 局_第一个程序资源段校验[2]
	局_易源码字节集[462] = 局_第一个程序资源段校验[3]

	局_待校验字节集 = 局_易源码字节集[415 : 415+92]
	//19730004C0CCD4EBA4CAD0ADC5CE041973000419730004197300041973000419730000000300000000000000EC3413D8611B000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	fmt.Printf("2校验data结果:" + strings.ToUpper(hex.EncodeToString(局_待校验字节集)) + "\n")
	局_第二个程序资源段校验 := GetCheckValue(局_待校验字节集)
	fmt.Printf("2校验值结果:" + strings.ToUpper(hex.EncodeToString(局_第二个程序资源段校验)) + "\n")
	局_易源码字节集[411] = 局_第二个程序资源段校验[0]
	局_易源码字节集[412] = 局_第二个程序资源段校验[1]
	局_易源码字节集[413] = 局_第二个程序资源段校验[2]
	局_易源码字节集[414] = 局_第二个程序资源段校验[3]
	return 局_易源码字节集
}

func 转换字节集并补齐32位(文本 string) []byte {

	转换后字节集 := []byte(文本)
	返回 := make([]byte, 32)
	copy(返回, 转换后字节集)
	for i := len(转换后字节集); i < 32; i++ {
		返回[i] = 0x20
	}
	return 返回
}

func 寻找字节集(原始字节集 []byte, 欲寻找的字节集 []byte) int {
	局_位置 := -1
	if len(欲寻找的字节集) == 0 {
		return 局_位置
	}
	局_长度 := len(欲寻找的字节集)
	for i := 0; i < (len(原始字节集) - 局_长度); i++ {
		if 原始字节集[i] == 欲寻找的字节集[0] {
			for a := 0; a < 局_长度; a++ {
				if 原始字节集[i+a] != 欲寻找的字节集[a] {
					break
				}
				if a == 局_长度 {
					局_位置 = i
				}
			}

			if 局_位置 != -1 {
				break
			}

		}
	}

	return 局_位置
}

// 校验值
func GetCheckValue(data []byte) []byte {
	keyLen := 4
	key := make([]byte, keyLen)
	dataLen := len(data)
	i := 0

	for i < dataLen {
		key[i%keyLen] ^= data[i]
		i++
	}

	return key
}
